<!DOCTYPE html>
<html>
<head>
    <title>模具换件记录</title>
    #parse("sys/header.html")
</head>
<body>
<div id="rrapp" v-cloak>
    <div v-show="showList" style="padding: 5px">
        <Row :gutter="16" style="margin: 8px 0px 0px 1px">
            <div class="search-group">
                <i-button @click="reload">刷新</i-button>
                #if($shiro.hasPermission("modulereplacepart:save"))
                <i-button type="info" @click="modPosition"><i class="fa fa-plus"></i>&nbsp;关联模具换件位置</i-button>
                <i-button type="info" @click="batchAssociateSP"><i class="fa fa-plus"></i>&nbsp;批量关联型芯</i-button>
                #end
                #if($shiro.hasPermission("modulereplacepart:delete"))
                <i-button type="error" @click="del"><i class="fa fa-trash-o"></i>&nbsp;删除</i-button>
                #end
            </div>
        </Row>
        <table id="jqGrid"></table>
    </div>
</div>
<script>
    $(function () {
        var queryUrl = '../modulereplacepart/list';
        var param = '';
        var replaceCode = getQueryString("replaceCode");
        if (replaceCode) {
            vm.moduleReplacePart.replaceCode = replaceCode;
            param += 'replaceCode=' + replaceCode;
        }
        typeMethod = getQueryString("typeMethod");
        if (typeMethod) {
            vm.moduleReplacePart.typeMethod = typeMethod;
            param += (param.length > 0 ? '&' : '') + 'typeMethod=' + typeMethod;
        }
        moldId = getQueryString("moldId");
        if (moldId) {
            vm.moduleReplacePart.moldId = moldId;
        }

        if (param.length > 0) {
            queryUrl += '?' + param;
        }
        $("#jqGrid").Grid({
            url: queryUrl,
            colModel: [
                {
                    label: '换件类别', name: 'typeMethod', index: 'TYPE_METHOD', width: 70,
                    formatter: function (value, col, row) {
                        return sysEnums.MODULE_TICKET_TYPE[row.typeMethod];
                    }
                },
                {label: '模具号', name: 'moduleNo', index: 'MODULE_NO'},
                {label: '备件名称', name: 'fittingName', index: 'FITTING_NAME', width: 80},
                {label: '位置号', name: 'positionNo', index: 'POSITION_NO', width: 80},
                {label: '类型', name: 'type', index: 'TYPE', width: 80},
                {
                    label: '更换时间', name: 'replaceDate', index: 'REPLACE_DATE', width: 100, formatter: function (value) {
                        return transDate(value, 'yyyy-MM-dd hh:mm');
                    }
                },
                {label: '寿命标准', name: 'lifeStandard', index: 'LIFE_STANDARD', width: 80},
                {label: '使用次数', name: 'realUseTime', index: 'REAL_USE_TIME', width: 80},
                {label: '型芯条码', name: 'uniqueCode', index: 'UNIQUE_CODE', sortable: false,
                    formatter: function (value, col, row) {
                        if(value){
                            return value;
                        }
                        return "<button class='btn btn-outline btn-info' id='recordsText' onclick='vm.associateSP(\"" + row.id + "\")'>&nbsp;关联</button>";
                    }
                },
                {label: '备注', name: 'remark', index: 'REMARK', width: 120},
                {label: '创建人', name: 'createUserName', index: 'CREATE_USER', width: 80},
                {
                    label: '当前进度', index: 'PROGRESS', width: 80,
                    formatter: function (value, col, row) {
                        return sysEnums.MOLD_REPAIR_PROGRESS[row.progress];
                    }
                },
                {name: 'progress', hidden: true},
                {name: 'moduleCode', hidden: true},
                {label: 'id', name: 'id', index: 'ID', key: true, hidden: true}
            ]
        });
    });

    var vm = new Vue({
        el: '#rrapp',
        data: {
            showList: true,
            title: null,
            moduleReplacePart: {
                typeMethod: '',
                replaceCode: '', // 工单编码,显示用
                moldPositionList: [],
                moldId: '',
            },
            q: {
                moduleNo: '',
                name: ''
            },
            moldTicketTypeList: enumsToList('MODULE_TICKET_TYPE')
        },
        methods: {
            /**
             * 模具位置关联
             */
            modPosition: function () {
                openWindow({
                    type: 2,
                    title: '<i class="fa fa-bars"></i> 模具关联位置列表',
                    content: '../module/modulpositioncorrelationForSelect.html?moldId=' + this.moduleReplacePart.moldId,
                    btn: ['确定', '取消'],
                    btn1: function (index) {
                        var iframeWin = window["layui-layer-iframe" + index];
                        // 需要关联型芯的模具关联位置列表
                        vm.moduleReplacePart.moldPositionList = iframeWin.getSelectedPos();
                        Ajax.request({
                            url: "../modulereplacepart/save",
                            params: JSON.stringify(vm.moduleReplacePart),
                            type: "POST",
                            contentType: "application/json",
                            successCallback: function (r) {
                                alert('操作成功', function () {
                                    // 关闭当前页面
                                    layer.close(index);
                                    vm.reload();
                                });
                            }
                        });
                    }
                })
            },
            // 关联模具型芯
            associateSP: function (recordId) {
                openWindow({
                    type: 2,
                    title: '<i class="fa fa-file-text-o"></i> 扫码型芯',
                    content: '../module/modulereplacepart_addSP.html',
                    btn: ['确定', '取消'],
                    btn1: function (index) {
                        var iframeWin = window["layui-layer-iframe" + index];
                        var moldSpUsedRecord = iframeWin.getMoldSpUsedRecord();
                        // 执行入库操作
                        Ajax.request({
                            url: "../modulereplacepart/associateMoldSpStock",
                            params: {'replacePartRecordId': recordId, 'stockId': moldSpUsedRecord.sparePartsId},
                            type: "POST",
                            successCallback: function (r) {
                                // 关闭当前页面
                                layer.close(index);
                                vm.reload();
                            }
                        });
                    }
                });
            },
            // 批量关联模具型芯
            batchAssociateSP: function (recordId) {
                openWindow({
                    type: 2,
                    title: '<i class="fa fa-file-text-o"></i> 批量关联型芯',
                    content: '../module/modulereplacepart_batchAddSP.html?typeMethod=' + this.moduleReplacePart.typeMethod + '&replaceCode=' + this.moduleReplacePart.replaceCode,
                    btn: ['确定', '取消'],
                    btn1: function (index) {
                        var iframeWin = window["layui-layer-iframe" + index];
                        var moldSpUsedRecord = iframeWin.getMoldSpUsedRecord();
                        // 执行入库操作
                        Ajax.request({
                            url: "../modulereplacepart/associateMoldSpStock",
                            params: {'replacePartRecordId': recordId, 'stockId': moldSpUsedRecord.sparePartsId},
                            type: "POST",
                            successCallback: function (r) {
                                // 关闭当前页面
                                layer.close(index);
                                vm.reload();
                            }
                        });
                    }
                });
            },
            query: function () {
                vm.reload();
            },
            del: function (event) {
                var ids = getSelectedRows("#jqGrid");
                if (ids == null) {
                    return;
                }
                var dataList = getSelectedRowDatas('#jqGrid');
                for (var i = 0; i < dataList.length; i++) {
                    if (dataList[i].progress == 5) {
                        layer.alert('只可以删除未通过审核的记录！', {icon: 5});
                        return;
                    }
                }
                confirm('确定要删除选中的记录？', function () {
                    Ajax.request({
                        url: "../modulereplacepart/delete",
                        params: JSON.stringify(ids),
                        type: "POST",
                        contentType: "application/json",
                        successCallback: function () {
                            alert('操作成功', function (index) {
                                vm.reload();
                            });
                        }
                    });
                });
            },
            getInfo: function (id) {
                Ajax.request({
                    url: "../modulereplacepart/info/" + id,
                    async: true,
                    successCallback: function (r) {
                        vm.moduleReplacePart = r.moduleReplacePart;
                    }
                });
            },
            reload: function (event) {
                vm.showList = true;
                var param = {};
                // 清空postData缓存数据
                var postData = $("#jqGrid").jqGrid("getGridParam", "postData");
                $.each(postData, function (k, v) {
                    delete postData[k];
                });
                var page = $("#jqGrid").jqGrid('getGridParam', 'page');
                $("#jqGrid").jqGrid('setGridParam', {
                    postData: param,
                    page: page
                }).trigger("reloadGrid");
                vm.handleReset('formValidate');
            },
            handleSubmit: function (name) {
                handleSubmitValidate(this, name, function () {
                    vm.saveOrUpdate()
                });
            },
            handleReset: function (name) {
                handleResetForm(this, name);
            },
            review: function () { //审核
                var result = getSelectedRowData("#jqGrid");
                var id = result.id;
                if (id == null) {
                    return;
                }
                if (result.reviewStatus === "未审核") {
                    vm.getInfo(id);
                    confirm('确定要审核选中的记录？', function () {
                        vm.moduleReplacePart.reviewStatus = 1;
                        Ajax.request({
                            url: '../modulereplacepart/review',
                            params: JSON.stringify(vm.moduleReplacePart),
                            type: "POST",
                            contentType: "application/json",
                            successCallback: function (r) {
                                alert('操作成功', function (index) {
                                    vm.reload();
                                });
                            }
                        });
                    });
                } else {
                    alert('本记录已审核!');
                }
            },
            getModuleArchives: function () {
                if (!this.moduleReplacePart.typeMethod) {
                    iview.Message.error("请选择工单类别后在执行该操作!");
                    return;
                }
                openWindow({
                    type: 2,
                    title: '<i class="fa fa-file-text-o"></i> 请选择工单关联模具',
                    content: '../module/moduleArchiveSelectInTicket.html?typeMethod=' + this.moduleReplacePart.typeMethod,
                    btn: ['确定', '取消'],
                    btn1: function (index) {
                        var iframeWin = window["layui-layer-iframe" + index];
                        var res = iframeWin.getModuleArchives();
                        if (!res) {
                            iview.Message.error("请选择一条记录");
                        } else {
                            if (res.ticketNo) {
                                vm.moduleReplacePart.replaceCode = res.ticketNo;
                            } else {
                                vm.moduleReplacePart.replaceCode = res.moduleCode;
                            }
                            vm.moduleReplacePart.moduleCode = res.moduleCode;
                            vm.moduleReplacePart.moduleNo = res.moduleNo;
                            vm.moduleReplacePart.moldId = res.moldId;
                            layer.close(index);
                        }
                    }
                })
            }
        }
    });
</script>
</body>
</html>
